//
//  ListViewVC.m
//  DMT_Print
//
//  Created by 牛魔王 on 2018/9/3.
//  Copyright © 2018年 庄文盛 All rights reserved.
//

#import "ListViewVC.h"
#import "BannerView.h"
#import "Header.h"
#import "XHToast.h"
#import "SWFormCommonController.h"

@interface ListViewVC ()<UIScrollViewDelegate>
@property (nonatomic, strong) BannerView *banner;
@property (nonatomic, strong) UITableView *listContent;
@property (nonatomic, strong) UIButton *deleteBtn;
@property (nonatomic, strong) UILabel *income;
@property (nonatomic, strong) UIView *line;
@end

@implementation ListViewVC

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    //去掉导航栏底部的黑线
    self.navigationController.navigationBar.shadowImage = [UIImage new];
}

- (void)viewWillDisappear:(BOOL)animated {
     self.navigationController.navigationBar.translucent = true;
}

- (void)viewWillAppear:(BOOL)animated {
   
    self.navigationController.navigationBar.translucent = false;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    //去掉导航栏底部的黑线
    self.navigationController.navigationBar.shadowImage = [UIImage new];
    UIButton *back=[[UIButton alloc]init];
    UIImage *img=[UIImage imageNamed:@"Chevron"];
    [back setImage:img forState:UIControlStateNormal];
    //    back.frame=CGRectMake(0, 0, 12.5, 21);
    [back addTarget:self action:@selector(toIndexView) forControlEvents:UIControlEventTouchDown];
    UIBarButtonItem *left=[[UIBarButtonItem alloc]initWithCustomView:back];
    self.navigationItem.leftBarButtonItem=left;
    
    self.title = @"任务详情";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.banner];
    self.banner.banner.delegate = self;
    [self.view addSubview:self.deleteBtn];
    [self.view addSubview:self.line];
    [self.view addSubview:self.income];

    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BannerView *)banner {
    if (!_banner) {
        _banner = [[BannerView alloc] initWithFrame:CGRectMake(0, 0, KSWITH, 230)];
        
    }
    return _banner;
}

- (UITableView *)listContent {
    if (!_listContent) {
        _listContent = [[UITableView alloc]initWithFrame:CGRectMake(0, 260, KSWITH, KSHEIGHT - 200 - 60) style:UITableViewStylePlain];
    }
    return _listContent;
}


- (UILabel *)income {
    if (!_income) {
        _income = [UILabel new];
        _income.text = [NSString stringWithFormat:@"合计葡萄 :%@",_model.amout];
        _income.frame = CGRectMake(20, KSHEIGHT-50-64,220, 40);
        _income.font = [UIFont systemFontOfSize:15];
    }
    return _income;
}

- (UIView *)line {
    if (!_line) {
        _line = [UIView new];
        _line.frame = CGRectMake(0,KSHEIGHT-60-64, KSWITH, 1);
        _line.backgroundColor = [UIColor colorWithHexString:@"#DCDCDC"];
    }
    return _line;
}

- (UIButton *)deleteBtn {
    if (!_deleteBtn) {
        _deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_deleteBtn setBackgroundImage:[UIImage imageNamed:@"but 7"] forState:UIControlStateNormal];
        _deleteBtn.frame = CGRectMake(KSWITH-110, KSHEIGHT-50-64, 90, 36);
        _deleteBtn.layer.masksToBounds = YES;
        _deleteBtn.layer.cornerRadius = 18;
        if (_listOrTask == 1) {
            [_deleteBtn setTitle:@"接受" forState:UIControlStateNormal];
            [_deleteBtn addTarget:self action:@selector(addAction) forControlEvents:UIControlEventTouchDown];
        }
        else {
            [_deleteBtn setTitle:@"提交" forState:UIControlStateNormal];
            [_deleteBtn addTarget:self action:@selector(completeAction) forControlEvents:UIControlEventTouchDown];
        }
    }
    return _deleteBtn;
}

- (void)completeAction {
    SWFormCommonController *commonVC = [[SWFormCommonController alloc]initWithStyle:UITableViewStyleGrouped];
    commonVC.model = self.model;
    commonVC.taskOrPrint = 1;
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:commonVC];
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)addAction {
    [SVProgressHUD showWithStatus:@"loading"];
    NSDictionary *parameters = @{@"id":_model.id,@"userId":[[NSUserDefaults standardUserDefaults] valueForKey:USERID]};
    AFHTTPSessionManager* session=[AFHTTPSessionManager manager];
    session.responseSerializer=[AFHTTPResponseSerializer serializer];
    
    [session POST:@"http://127.0.0.1:3000/list" parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [SVProgressHUD dismiss];
        [XHToast showCenterWithText:@"已接受任务"];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"失败");
        
    }];
}

- (void)toIndexView {
    [self.navigationController popViewControllerAnimated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
